﻿using HealthcareSystem.API.Read.Application.Command.ChronicDisease;
using HealthcareSystem.API.Read.DTO.ChronicDTO;
using HealthcareSystem.Domain;
using HealthcareSystem.Domain.ChronicDiseaseManagement;
using MediatR;
using WeekExam.PassPort.ErrorCode;
using WeekExam.PassPort.Infrastructure.Interface;

namespace HealthcareSystem.API.Read.Application.CommandHandel.ChronicDisease
{
    public class FollowupManagementShowCommandHandler : IRequestHandler<FollowupManagementShowCommand, ApiResult<ApiPaging<List<FollowupDTO>>>>
    {
        private readonly IBaseRepository<ServiceRecord> srRepository;

        private readonly IBaseRepository<Record> reRepository;

        public FollowupManagementShowCommandHandler(IBaseRepository<ServiceRecord> srRepository, IBaseRepository<Record> reRepository)
        {
            this.srRepository = srRepository;
            this.reRepository = reRepository;
        }

        public Task<ApiResult<ApiPaging<List<FollowupDTO>>>> Handle(FollowupManagementShowCommand request, CancellationToken cancellationToken)
        {
            ApiResult<ApiPaging<List<FollowupDTO>>> result = new ApiResult<ApiPaging<List<FollowupDTO>>>();
            result.Code = ResultCode.查询成功;
            result.Message = ResultCode.查询成功.ToString();
            result.Code = ResultCode.查询成功;
            result.Message = ResultCode.查询成功.ToString();

            var list = from a in srRepository.GetAll()
                       join b in reRepository.GetAll() on a.RecordId equals b.RecordId
                       select new FollowupDTO()
                       {
                           FollowupId = a.FollowupId ,
                           DiseaseType = a.DiseaseType,
                           FollowupTime = a.FollowupTime,
                           FollowupType = a.FollowupType,
                           FollowupState = a.FollowupState,
                           RecordId = b.RecordId,
                           RecordName = b.RecordName,
                           RecordSex = b.RecordSex,
                           RecordAge = b.RecordAge,
                           RecordIdcard = b.RecordIdcard,
                           RecordPhone = b.RecordPhone,
                           RecordSite = b.RecordSite,
                           RecordTeam = b.RecordTeam,
                           RecordState = b.RecordState,
                           RecordDate = b.RecordDate,
                           RecordMarriage = b.RecordMarriage,
                           RecordBlood = b.RecordBlood,
                           RecordInsured = b.RecordInsured,
                           RecordProfession = b.RecordProfession,
                           RecordCulture = b.RecordCulture,
                       };
            var totalCount = list.Count();
            var totalPage = (int)Math.Ceiling((totalCount * 1.0) / request.PageSize);
            list = list.OrderByDescending(x => x.FollowupId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
            ApiPaging<List<FollowupDTO>> paging = new ApiPaging<List<FollowupDTO>>();
            paging.totalPage = totalPage;
            paging.totalCount = totalCount;
            paging.Data = list.ToList();
            result.Data = paging;
            return Task.FromResult(result);
        }
    }
}
